#include "bimoscontrol.h"


//DEBUG
#include <QFile>

#include "Model/bimospackage.h"


BimosControl::BimosControl(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::BimosControl)
{
    ui->setupUi(this);
    setupGraphics();
    wizard = new NewProject();
    wizardCal = new Calibration();

}

BimosControl::~BimosControl()
{
    delete ui;
}

void BimosControl::setupGraphics() {
    bGraphics = new View(ui);
    connect(bGraphics->ui->clBtn_newProject,SIGNAL(clicked()),this,SLOT(showWizard()));
    connect(bGraphics->ui->clBtn_contiueProject,SIGNAL(clicked()),this,SLOT(showWizard()));
    connect(bGraphics->ui->clBtn_calibrate,SIGNAL(clicked()),this,SLOT(showCalibration()));
}




void BimosControl::setupBimosBox() {
    bHandler = new Connectivity();
    connect(bHandler->dataGrabber, SIGNAL(timeout()), bHandler->dataGrabber, SLOT(shot()));
    connect(bHandler->dataGrabber,SIGNAL(lastConnectorActive(int)),bGraphics,SLOT(setLastConnectorActive(int)));
    connect(bHandler->dataGrabber,SIGNAL(newVoltValue(int,float)),this,SLOT(drawVolt(int,float)));
    for (int i = 0; i < COUNTOFCONNECTORS; i++) {
        connect(bGraphics->displayBottle[i],SIGNAL(toggled(bool)),this,SLOT(toggleConnector(bool)));
    }

}

void BimosControl::showWizard() {

    wizard->exec();
    config = wizard->getConfig();
    initializePackages();
    setupBimosBox();
    bGraphics->ui->clBtn_calibrate->setEnabled(true);
}

void BimosControl::showCalibration() {
    wizardCal->setConfig(config);
    wizardCal->exec();
    initializePackages();
}

void BimosControl::drawVolt(int conNumber,float value) {
    wizardCal->setConnectorValue(conNumber,value);
    bGraphics->drawVoltValue(conNumber,value);
    bGraphics->setTextValues(conNumber,value,config->getMultiplicator()[conNumber],config->getOffset()[conNumber]);

}

void BimosControl::toggleConnector(bool checked) {
    for (int i = 0; i < COUNTOFCONNECTORS; i++) {
        if (bGraphics->displayBottle[i]->isChecked())
            bHandler->dataGrabber->connectorToggled(i,true);
        else
            bHandler->dataGrabber->connectorToggled(i,false);
    }

}

void BimosControl::initializePackages() {
    //BimosPackage::setvolumeBottle(config->volumeBottles);
    //BimosPackage *a = new BimosPackage;
}

